#!/bin/sh

# Release - 02/07 #

############################ SupTime Project v0.1a ###############################
# Hi, this is really-easy bash script which generates png image with your system #
# top and current uptime.                                                        #
##################################################################################
# Please consider that it is an early alpha version of script which I'm writing  #
# _FOR_PERSONAL_USE_ - so don't wait anything great. Also my linux knowledge is  #
# ather poor, so it is sure that there are many things in this script which      #
# could be improved...                                                           #
##################################################################################
# Script requires:                                                               #
# - ImageMagick >= 6.2.5                                                         #
# - uptimed                                                                      #
# - uprecords (uprecords-cgi)                                                    #
##################################################################################
# Contact? >> sufixx[at]gmail.com                                                #
##################################################################################


# Configurable variables

TMP_DIR=/tmp
BASE_DIR=`pwd` # of course - put here normal path!
THEME=theme_alpha
BACKGROUND=$BASE_DIR/$THEME/bg.png
FOREGROUND=$BASE_DIR/$THEME/fg.png
OUT=$BASE_DIR/out.png
# source $BASE_DIR/$THEME/theme.conf

# Read uptime variable from uprecords
UPTIME=`uprecords -a | grep "\->" | awk '{print $3 $4 $5 }' | sed 's/\days/ days/' |sed 's/\,/ /'`

# Read top uptime variable from uprecords
TOP=`uprecords | head -n 3 | tail -n 1 | awk '{print $2 $3 $4}' | sed 's/\days/ days/' |sed 's/\,/ /'`

# Save to temp file
echo '' > $TMP_DIR/up.txt
echo "   Uptime: $UPTIME" >> $TMP_DIR/up.txt
echo '' >> $TMP_DIR/up.txt
echo "   Record: $TOP" >> $TMP_DIR/up.txt
echo '' >> $TMP_DIR/up.txt
echo "   Kernel:  `uname -r`" >> $TMP_DIR/up.txt
#echo '' >> $TMP_DIR/up.txt
echo "   Host:      `uname -n`" >> $TMP_DIR/up.txt

# Render image with text from text file
cat $TMP_DIR/up.txt | convert -background transparent -fill black -font /usr/share/fonts/truetype/freefont/FreeSans.ttf label:@- -append $TMP_DIR/text.png

# Merge graphical background, text image and foreground
composite $TMP_DIR/text.png -compose Over $BACKGROUND $TMP_DIR/compisite.png
composite $FOREGROUND -compose Over $TMP_DIR/compisite.png $BASE_DIR/out.png

# Cleanup
rm -f $TMP_DIR/compisite.png $TMP_DIR/up.txt $TMP_DIR/text.png

# Upload image -- not yet - scp? ntftp?

exit;
